<?php
class GameEditor extends FormField
{
	
    function __construct($controller, $name, $title = null, $value = null, $form = null, $rightTitle = null) {
        $this->controller = $controller;
        parent::__construct($name, $title, $value, $form, $rightTitle);
    }
    
    public function FieldHolder()
    {
        
        Requirements::css("leagueManager/css/FieldEditor.css");
        return $this->renderWith("GameEditor");
    }

    public function Leagues() 
    {
        //Requirements::javascript("leaguemanager/javascript/LeagueEditor.js");
        Requirements::javascript("leagueManager/javascript/GameEditor.js");
        return $this->controller->Leagues();
        //$objectManager= new ManyManyDataObjectManager($l,'Teams','League',array('Title'=>'Title'));
        //return $objectManager->renderWith('DataObjectManager');
    }
}

class GameEditor_Controller extends Controller {

	public function showRound($request) {
		$leagueId = intval($_POST['leagueId']);
		$round = intval($_POST['round']);
        $league = DataObject::get_one('League','`League`.ID = ' . $leagueId);
        $league->Round = $round;
        return $league->GameTable();
	}
	
    public function saveGame($request) {

    	//a little hack sorry to set the locale right for the calendardatepicker
        if( $member = Member::currentUser() ) {
            i18n::set_locale($member->Locale);
        }
    	
        $gameId = intval($_POST['gameId']);
        $data = array();
       
        $homeTeamId = intval($_POST['HomeTeam']);
        $guestTeamId = intval($_POST['GuestTeam']);
        $refereeTeamId = intval($_POST['RefereeTeam']);
        $gameTime = $_POST['GameTime'];
        $gameDate = $_POST['GameDate'];
        $goalsHome = $_POST['GoalsHome'];
        $goalsGuest = $_POST['GoalsGuest'];
        $fpHome= $_POST['FPHome'];
        $fpGuest= $_POST['FPGuest'];
        
        $game = DataObject::get_one('Game','`Game`.ID = ' . $gameId);
        $game->HomeTeamID = $homeTeamId;
        $game->GuestTeamID = $guestTeamId;
        $game->RefereeTeamID = $refereeTeamId;
        $game->GameTime = $gameTime;
        $game->GoalsHomeTeam = $goalsHome;
        $game->GoalsGuestTeam = $goalsGuest;
        $game->FPHome = $fpHome;
        $game->FPGuest = $fpGuest;
        $dp = null;
        if ($gameDate) {
	        $dp = new CalendarPickerField('temp');
	        $dp->setValue($gameDate);
            $game->GameDate = $dp->dataValue();
        }
        $game->write();
        if ($gameDate && !isset($_POST['ChangeThis'])) {
	        $otherGames = DataObject::get('Game','`Game`.Round = '. $game->Round . ' AND `Game`.ID != ' . $gameId);
	
	        foreach ($otherGames AS $oGame) {
	        	$oGame->GameTime = $gameTime;
	        	$oGame->GameDate = $dp->dataValue();
	        	$oGame->write();
	        }
        }
        $league = DataObject::get_one('League','`League`.ID = ' . $game->LeagueID);
        $league->updateTable();
        $league->Round = $game->Round;
        return $league->GameTable();
        
    }
	
}
?>